' Lyrics Plugin Search script
'
' This script is a plug-in into MediaMonkey Web Search dialog. You should save it to Scripts folder as
' lyricsplugin.vbs. It has to be in Scripts.ini file, where entries can be as follows:
'
' [SearchAMG]
' FileName=lyricsplugin.vbs
' ProcName=lyricsplugin
' Order=99
' DisplayName=lyricsplugin
' Language=VBScript
' ScriptType=3

' Global
Dim Browser
Dim Tmr
Dim Current

Sub StartSearch(Panel, SearchTerm, SearchArtist, SearchAlbum)
     Set Browser = SDB.UI.NewActiveX(Panel, "Shell.Explorer")
     Browser.Common.Align = 5 

     Dim Tracks
     Dim Results

     Set Tracks = SDB.Tools.WebSearch.NewTracks
     Set Results = SDB.NewStringList
    
     For i = 0 to Tracks.Count-1
          Results.Add Tracks.item(i).ArtistName & " - " & Tracks.item(i).Title
     Next

     SDB.Tools.WebSearch.SetSearchResults Results
     If Results.Count > 0 Then
          SDB.Tools.WebSearch.ResultIndex = 0
     End If
End Sub

Sub ShowResult(ResultID)
     If (ResultID >= 0)  Then
          Browser.SetHTMLDocument ""

          With SDB.Tools.WebSearch
               .ClearTracksData 
               .TrackChecked(ResultID) = True
               Set Track = .NewTracks.Item(ResultID)
          End With
          
          With Track
               title = .Title
               artist = .ArtistName
          End With
          
          url = "http://www.lyricsplugin.com/winamp03/plugin/?title=" & title & "&artist=" & artist
          Browser.Interf.Navigate url
          
          Set Tmr = SDB.CreateTimer(1500)
          Script.RegisterEvent Tmr, "OnTimer", "WebsiteLoaded"
     End If
     
     dim Doc
End Sub

Sub WebsiteLoaded(Timer)
     Script.UnregisterEvents Tmr
     Set Tmr = Nothing

     Set Doc = Browser.Interf.Document
     If IsObject(Doc) Then
          Set lyrics = Doc.getElementById("lyrics")
          If IsObject(lyrics) Then
               For i = 0 To SDB.Tools.WebSearch.NewTracks.Count - 1
                    If SDB.Tools.WebSearch.TrackChecked(i)  Then
                         SDB.Tools.WebSearch.NewTracks.Item(i).Lyrics = lyrics.innerText
                    End If
               Next
               SDB.Tools.WebSearch.RefreshViews 
          End If
     End If
End Sub

Sub FinishSearch(Panel)
     Browser.Common.DestroyControl      
     Set Browser = Nothing              
End Sub